import VueRouter from "vue-router";
import Vue from "vue";
import Home from "../pages/Home.vue"
import Login from "../pages/Login.vue"
import Detail from "../pages/Detail.vue"
import City from "../pages/City.vue"
import Movie from "../pages/Movie.vue"
import Cinema from "../pages/Cinema.vue";
import Mine from "../pages/Mine.vue";
import CinemaDetail from "../pages/CinemaDetail.vue";
// import Comingsoon from '../pages/Comingsoon.vue'



// 给vue 安装路由插件
Vue.use(VueRouter)
// 创建路由实例对象
const router = new VueRouter({
    mode:'hash',
    // 配置路由
    routes:[
        // redirect:重定向
        {path:'/',redirect:'/home'},
        // 配置路由器规则
        {
            path:'/home' , 
            name:'home' , 
            component:Home,
            // 重定向
            redirect:'/home/movie',
            // 配置子路由
            children:[
                {
                    path:'/home/movie', 
                    name:'movie', 
                    component:Movie,
                    meta:{
                    title:'电影'
                    }
                    // children:[
                    //     {path:'/home/movie/Comingsoon', name:'Comingsoon', component:Comingsoon}
                    // ]
                },
                {
                    path:'/home/cinema', 
                    name:'cinema', 
                    component:Cinema,
                    meta:{
                        title:'影院'
                    }
                },
                {
                    path:'/home/mine', 
                    name:'mine', 
                    component:Mine,
                    meta:{
                        // 谁需要拦截  谁加上标识
                        isAuth:true,
                        title:'个人中心'
                    }
                }
            ]
        },
        {
            // 动态路由 参数是path的一部分
            path:'/detail/:id' , 
            name:'detail' , 
            component:Detail,
            meta:{
                title:'详情',
                isAuth:true
            }
        },
        {
            path:'/cinema/detail/:id' , 
            name:'cinemaDetail' , 
            component:CinemaDetail,
            meta:{
                title:'影城详情',
                isAuth:true
            }
        },
        {
            path:'/login' , 
            name:'login' , 
            component:Login,
            meta:{
                title:'登录'
            }
        },
        {
            path:'/city' , 
            name:'city' , 
            component:City,
            // meta中的数据可以在路由信息中获取到
            meta:{
                title:'城市页面',
                isAuth:true
            }
        }
    ]
})

//全局前置守卫
router.beforeEach((to,from,next)=>{
//    console.log(to,from)
    // //拦截数量较少
//    if(to.name ==='mine' || to.name === 'city') {
//     const token = localStorage.getItem('token')
//     if(!token) {
//         next('/login')
//         return
//         }
//    }

    if(to.meta.title) {
        document.title = to.meta.title
    }
    // 拦截数量多
    // console.log('to', to)
    if(to.meta.isAuth) {
        // 获取登录的本地存储
        const token = localStorage.getItem('token')
        if(!token) {
            next({
                path:'/login',
                query:{
                    redirectUrl:to.fullPath
                }
            })
            return
            }
        }
   next()
})

export default router